const app = getApp();
let abnormal, normal, online, offline;
const pageSize = 15;
const statusList = [{
  id: 0,
  label: '全部'
}, {
  id: 1,
  label: '在线'
}, {
  id: 2,
  label: '离线'
}];
const noticeList = [{
    id: '',
    label: '全部'
  },
  {
    id: 1,
    label: '正常'
  }, {
    id: 2,
    label: '低水位'
  }, {
    id: 3,
    label: '低压'
  }, {
    id: 4,
    label: '高压'
  }, {
    id: 5,
    label: '机箱内温度低'
  },
  {
    id: 6,
    label: '机箱内温度高'
  }
];

Page({
  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
    loadmore: false, //加载更多
    nomore: false, //没有更多
    list: [], //列表数据
    drawer: false, //展开搜索抽屉
    isFilter: false, // 是否有筛选条件
    equipment_keyword: '',
    start_time: '',
    end_time: '',
    statusList,
    statusIndex: "",
    noticeList,
    noticeIndex: '',
    total: 0,
    offline: 0,
    online: 0
  },

  onPullDownRefresh: function () {
    this.getUserData();
    this.reloadData();
  },


  showDrawer() {
    this.condition = {
      equipment_keyword: this.data.equipment_keyword,
      noticeIndex: this.data.noticeIndex,
      statusIndex: this.data.statusIndex,
      start_time: this.data.start_time,
      end_time: this.data.end_time
    }
    this.setData({
      drawer: true,
    })
  },


  closeDrawer() {
    this.setData({
      drawer: false,
      ...this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    this.setData({
      equipment_keyword: "",
      noticeIndex: "",
      statusIndex: "",
      start_time: "",
      end_time: ""
    })
  },
  confirmDrawer() {
    let isFilter = false;
    if (this.data.equipment_keyword != "" ||
      this.data.noticeIndex !== "" ||
      this.data.statusIndex !== "" ||
      this.data.start_time != "" ||
      this.data.end_time != ""
    ) {
      isFilter = true;
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },




  navParamSetting(e) {
    wx.navigateTo({
      url: '/package/pages/parameterSetting/index?sn=' + e.currentTarget.dataset.sn,
    })
  },

  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [name]: index === this.data[`${name}`] ? '' : index
    })
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    });
  },


  copyEqiupmentNumber(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let navTitle;
    let type = options.type,
      parameter = false;
    if (type) {
      parameter = true;
      if (type == 1) { //正常
        navTitle = "正常设备";
        this.eq_status_another = 1;
      } else if (type == 2) { //异常
        navTitle = "异常设备";
        this.eq_status_another = -1;
      } else if (type == 3) { //异常
        navTitle = "在线设备";
        this.stutus_another = 1;
      } else if (type == 4) { //异常
        navTitle = "离线设备";
        this.stutus_another = 2;
      }
      wx.setNavigationBarTitle({
        title: navTitle
      })
    }
    this.setData({
      parameter
    })
    this.page = 1;
    this.getUserData();
    this.getList();
  },



  getUserData() {
    app.ajax({
      url: 'Manage/User/getManageInfo',
      method: "POST",
      data: {},
      success: res => {
        if (res.data.code == 200) {
          this.setData({
            is_auth: res.data.data.is_auth
          })
        } else {
          app.showToast("获取用户信息失败");
        }
      }
    })
  },

  reloadData() {
    this.page = 1;
    this.setData({
      nomore: false,
      empty: false,
      loadmore: false,
      list: [],
      total: 0,
      online: 0,
      offline: 0
    })
    wx.showLoading({
      mask: true,
    })
    this.getList();
  },

  //picker选择类型
  bindPickerChange(e) {
    const typeIndex = e.detail.value;
    this.setData({
      typeIndex: Number(typeIndex)
    })
  },

  onReady: function () {
    this.cascade = this.selectComponent("#cascade")
  },

  showCascade() {
    this.cascade.selection(this.data.area_id_Info);
  },

  cascadeComplete(e) {
    let {
      ids,
      names
    } = e.detail;
    this.setData({
      areaInfo_name: names,
      area_id_Info: ids
    })
  },

  //选择地址
  addressChange(e) {
    console.log(e);
    this.setData({
      areaInfo_name: e.detail.address.areaInfo_name,
      area_id_Info: e.detail.address.area_id_Info,
    });
  },

  //监听搜索输入
  bindInput(e) {
    const typeIndex = this.data.typeIndex;
    const val = e.detail.value;
    if (typeIndex === 0) {
      this.setData({
        villageName: val
      })
    } else {
      this.setData({
        eqSn: val
      })
    }
  },




  navRegionCode(e) {
    wx.navigateTo({
      url: '../regionCode/list/list?equipment_id=' + e.currentTarget.dataset.id,
    })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore == true) {
      ++this.page;
      this.getList();
    }
  },



  getList() {
    let notice = '',
      status = '';
    if (this.data.noticeIndex !== '') {
      notice = this.data.noticeList[this.data.noticeIndex].id;
    }
    if (this.data.statusIndex !== '') {
      status = this.data.statusList[this.data.statusIndex].id;
    }
    app.ajax({
      url: 'Manage/Equipment/getList',
      data: {
        page: this.page,
        row: pageSize,
        equipment_keyword: this.data.equipment_keyword,
        equipment_status: this.data.parameter ? (this.eq_status_another || '') : notice,
        status: this.data.parameter ? (this.stutus_another || '') : status,
        start_time: this.data.start_time,
        end_time: this.data.end_time
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code == 1000) {
          let source = res.data.data;
          let list = this.data.list.concat(source.eq);
          this.setData({
            nomore: list.length >= source.count ? true : false,
            loadmore: list.length < source.count ? true : false,
            list,
            off_number: source.off_number,
            on_number: source.on_number,
            total: source.count,
            drawer: false
          })
        } else {
          if (this.page == 1) {
            this.setData({
              empty: true,
              list: [],
              drawer: false,
              loadmore: false,
              nomore: false
            })
          } else {
            this.setData({
              loadmore: false,
              nomore: true,
            })
          }
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
      }
    })
  },



  //跳转到详情
  navDetail(e) {
    const eq_num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: '../deviceDetail/deviceDetail?eq_sn=' + eq_num,
    })
  },

  //修改信息
  editDeviceInfo(e) {
    const eq_num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: '../editDeviceInfo/editDeviceInfo?eq_sn=' + eq_num,
    })
  },

  navQrcode(e) {
    const eq_num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: '../deviceQrcode/deviceQrcode?eq_sn=' + eq_num,
    })
  },




  //打开地图
  navigateAddress(e) {
    let {
      longitude,
      latitude
    } = e.currentTarget.dataset;
    longitude = Number(longitude);
    latitude = Number(latitude);
    if (latitude == 0 || latitude == '' || longitude == 0 || longitude == '') {
      app.showToast("位置信息不全,请换个园区重试", "none", 2000, function () {});
      return false;
    }
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18
    })
  },

  //移动movable-view
  movableChange(e) {
    let {
      x,
      y
    } = e.detail;
    this.setData({
      x,
      y
    })
  },


  navConsumeRecord(e) {
    const eq_num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: '../consumeList/consumeList?eq_sn=' + eq_num,
    })
  },



})